package com.sphd.emanu.activity.goodstype;

import android.support.v7.app.AlertDialog;

import com.google.gson.JsonObject;

import com.sphd.emanu.mvp.BasePresenterImpl;
import com.sphd.emanu.pojo.goods.Category;
import com.sphd.emanu.utils.GsonUtil;
import com.wzutils.JsonUtil;
import com.wzutils.L;

import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
 * @Author Hu
 * @Time 2017/5/27 -- 12:06.
 * @Email 87683202@qq.com
 */

public class GoodstypePresenter extends BasePresenterImpl<GoodstypeContract.View> implements GoodstypeContract.Presenter {

    @Override
    public void getList(int id) {
        Call<String> call = emanuService.classfylist(id);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                L.e(response.body());
               List<Category>  catelist= GsonUtil.jsonToList(response.body().toString(),Category.class);
                mView.setObject(catelist);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void addCategory(String name, int org, final AlertDialog alertDialog) {
        Call<JsonObject> call =emanuService.addCategory(name,org);
        call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                int res=response.body().get("resStatus").getAsInt();
                mView.setRes(res,alertDialog);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void updateCategory(int id, String name, int org, final AlertDialog alertDialog) {
        Call<JsonObject> call =emanuService.updateCategory(id,name,org);
        call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                int res=response.body().get("resStatus").getAsInt();
                mView.setRes(res,alertDialog);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void deleteCategory(int id, final SweetAlertDialog alertDialog) {
        Call<JsonObject> call =emanuService.deleteCategory(id);
        call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                int res=response.body().get("resStatus").getAsInt();
                mView.setDelRes(res,alertDialog);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }
}
